package com.panopset.gui;

import static com.panopset.Util.centerInRect;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * Usually used for about boxes, a PopupBox is a modeless dialog that disappears
 * when the user does a mouse click or presses any key.
 *
 * @author Karl Dinwiddie
 *
 */
public abstract class PopupBox {

    /**
     * Parent frame.
     */
    private JFrame parent;

    /**
     * Internal component.
     */
    private final Component component;

    /**
     * @return Size.
     */
    protected abstract Dimension createSize();

    /**
     * @param innerComponent to display
     */
    public PopupBox(final Component innerComponent) {
        component = innerComponent;
    }

    /**
     * @param innerComponent to display
     * @param parentFrame PopupBox will center on this frame
     */
    public PopupBox(final Component innerComponent, final JFrame parentFrame) {
        this(innerComponent);
        parent = parentFrame;
    }

    /**
     * Dispose popup box.
     */
    public final void dispose() {
        if (dialog != null) {
            dialog.dispose();
            dialog = null;
        }
    }

    /**
     * Show popup box.
     */
    public final void show() {
        getDialog().setVisible(true);
    }

    /**
     * Popup box dialog.
     */
    private JDialog dialog;

    /**
     * @return Popup box dialog.
     */
    private JDialog getDialog() {
        if (dialog == null) {
            dialog = new JDialog();
            dialog.setUndecorated(true);
            dialog.setSize(createSize());
            if (parent != null) {
                dialog.setLocation(centerInRect(dialog.getSize(),
                    parent.getBounds())); //parent = getFlexFrame().getFrame()
            }
            dialog.add(component);
            dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            dialog.addKeyListener(new KeyListener() {

                public void keyPressed(final KeyEvent e) {
                    PopupBox.this.dispose();
                }

                public void keyReleased(final KeyEvent e) {
                }

                public void keyTyped(final KeyEvent e) {
                }

            });
            dialog.addFocusListener(new FocusListener() {

                public void focusGained(final FocusEvent e) {
                }

                public void focusLost(final FocusEvent e) {
                    PopupBox.this.dispose();
                }

            });
            dialog.addMouseListener(new MouseListener() {

                public void mouseClicked(final MouseEvent e) {
                }

                public void mouseEntered(final MouseEvent e) {
                }

                public void mouseExited(final MouseEvent e) {
                }

                public void mousePressed(final MouseEvent e) {
                    PopupBox.this.dispose();
                }

                public void mouseReleased(final MouseEvent e) {
                }

            });
        }
        return dialog;
    }
}
